package com.wyx.controller.system;

import com.wyx.aspectj.annotation.Log;
import com.wyx.aspectj.enums.BusinessType;
import com.wyx.dto.DictDataDto;
import com.wyx.service.DictDataService;
import com.wyx.utils.ShiroSecurityUtils;
import com.wyx.vo.AjaxResult;
import com.wyx.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @author xiaoxiong
 * @version 1.0
 * @date 2021/2/28 14:21
 * 字典数据的增删改查
 */
@RestController
@RequestMapping("system/dict/data")
public class DictDataController {
    @Autowired
    private DictDataService dictDataService;

    /**
     * 分页查询字典数据
     * @param dictDataDto
     * @return
     */
    @GetMapping("/listForPage")
    public AjaxResult listForPage(DictDataDto dictDataDto){
        DataGridView dataGridView = this.dictDataService.listPage(dictDataDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }


    /**
     * 添加字典数据
     * @param dictDataDto
     * @return
     */
    @PostMapping("/addDictData")
    @Log(title = "添加字典数据",businessType = BusinessType.INSERT)
    public AjaxResult addDictData (@Validated DictDataDto dictDataDto){
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictDataService.insert(dictDataDto));
    }


    /**
     * 根据字典数据ID查询字典数据
     * @return
     */
    @GetMapping("/getOne/{dictCode}")
    public AjaxResult getDictData(@Validated @PathVariable @NotNull(message = "字典数据id不能为空") Long dictCode){
        return AjaxResult.success(this.dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 更新字典数据
     * @param dictDataDto
     * @return
     */
    @PutMapping("/updateDictData")
    @Log(title = "更新字典数据",businessType = BusinessType.UPDATE)
    public AjaxResult update(@Validated DictDataDto dictDataDto){
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.success(this.dictDataService.update(dictDataDto));
    }


    /**
     * 根据Ids 遍历删除字典数据
     * @param dictCodeIds
     * @return
     */
    @DeleteMapping("/deleteDictDataByIds/{dictCodeIds}")
    public AjaxResult deleteDictDataByIds(@Validated @PathVariable @NotEmpty(message = "字典的数据id不能为空") Long[] dictCodeIds){
        return AjaxResult.toAjax(this.dictDataService.deleteDictDataByIds(dictCodeIds));
    }

    /**
     * 根据字典数据类型删除字典
     * @param dictType
     * @return
     */
    @GetMapping("/getDataByType/{dictType}")
    @Log(title = "删除字典数据",businessType = BusinessType.DELETE)
    public AjaxResult getDataByType(@Validated @PathVariable @NotEmpty(message = "字典数据类型不能为空" ) String dictType){
        return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
    }
}
